package com.boyiis.sell.controller;

import com.boyiis.sell.enums.ResultEnum;
import com.boyiis.sell.model.entity.Product;
import com.boyiis.sell.model.entity.ProductCategory;
import com.boyiis.sell.model.form.ProductCategoryForm;
import com.boyiis.sell.model.form.ProductForm;
import com.boyiis.sell.service.ProductCategoryService;
import com.boyiis.sell.service.ProductService;
import com.boyiis.sell.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/seller/product/category")
@Slf4j
public class SellerProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 商品类目列表
     *
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list() {

        List<ProductCategory>  productCategoryList = productCategoryService.findAll();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("productCategoryList", productCategoryList);

        return new ModelAndView("product/category/list", map);
    }

    @GetMapping("/edit")
    public ModelAndView index(@RequestParam(value = "categoryId", required = false) Integer categoryId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", "/sell/seller/product/category/list");

        if (null != categoryId) {
            ProductCategory productCategory = productCategoryService.findOne(categoryId);
            map.put("productCategory", productCategory);
        }

        return new ModelAndView("product/category/edit", map);

    }

    /**
     * 保存/更新
     *
     * @param form
     * @param bindingResult
     * @return
     */
    @PostMapping("/save")
    public ModelAndView save(@Valid ProductCategoryForm form, BindingResult bindingResult) {
        Map<String, Object> map = new HashMap<String, Object>();

        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/product/category/edit");
            return new ModelAndView("common/error", map);
        }

        try{
            ProductCategory productCategory = new ProductCategory();
            if(null != form.getCategoryId()){
                productCategory = productCategoryService.findOne(form.getCategoryId());
            }
            BeanUtils.copyProperties(form, productCategory);
            productCategory = productCategoryService.save(productCategory);
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", e.getMessage());
            if(null != form.getCategoryId()){
                map.put("url", "/sell/seller/product/category/edit");
            }else{
                map.put("url", "/sell/seller/product/category/edit?categoryId=" + form.getCategoryId());
            }

            return new ModelAndView("common/error", map);
        }

        map.put("url", "/sell/seller/product/category/list");
        return new ModelAndView("common/success", map);

    }



}
